In this lesson, we will go thought the Label Control and some of its important properties and event.
Introduction to C# Label Control
the Label Control is probably the most used control in a WinForms Applications. It’s used to have a text in it, to label something.
Add a label to your Form
To add a label, simply drag and drop it to your Form from the toolbox.
Label Properties
Below are some of the most used Label Control properties. You can either set the property of a label by using the property menu, or by writing code in the Form Load
Name – Is the name or Identifier of the label. A good practice is to start the label name by the 3 letters ‘lbl’, such as lblFirstName, lblAddress.
private void frmMain_Load(object sender, EventArgs e) { label1.Name = "lbl"; }
Text – This property is why Labels are used. It’s the text that will be displayed inside the label. For example:
private void frmMain_Load(object sender, EventArgs e) { lblName.Text = "TutorialsPanel"; }
Font – To change the font of a label. For example:
lblName.Font = new Font("Arial", 16);
As the Font property cannot be changed once it’s created, we will instead create a new font and reassigned it to the font property of the Label.
Visible – A boolean property that toggles the visibility of the Label. For example, the code below will hide the label from showing up on the form. The default value of this property is true.
// Label Visibility lblName.Visible = false;
Label Events
Two events are worth to mention for the Label control are the Click and Mouse Enter Events:
Label Click Event
private void lblName_Click(object sender, EventArgs e) { lblName.Text = "Mouse Click Event!"; }
Label Mouse Enter Event
private void lblName_MouseEnter(object sender, EventArgs e) { lblName.Text = "Mouse Enter Event!"; }
In the next lesson, you will learn about another important control from the C# User Interface library, which is the button Control.
All C# Tutorials | Tutorial Home | >> C# Button Control |
Comments